package models

import (
	"el/app/models/request"
	"el/app/models/response"
	"el/bootstrap/driver"

	"gorm.io/gorm"
)

// 管理员日志表
type AdminLog struct {
	Id        uint   `gorm:"column:id" db:"id" json:"id" form:"id"`
	Path      string `gorm:"column:path" db:"path" json:"path" form:"path"`                 //路由
	Param     string `gorm:"column:param" db:"param" json:"param" form:"param"`             //请求参数
	AdminId   int    `gorm:"column:admin_id" db:"admin_id" json:"admin_id" form:"admin_id"` //管理员
	CreatedAt MyTime `gorm:"column:created_at" db:"created_at" json:"created_at" form:"created_at"`
	UpdatedAt MyTime `gorm:"column:updated_at" db:"updated_at" json:"updated_at" form:"updated_at"`
	HttpCode  int    `gorm:"column:http_code" db:"http_code" json:"http_code" form:"http_code"` //返回状态
	Ip        string `gorm:"column:ip" db:"ip" json:"ip" form:"ip"`                             //ip
	Method    string `gorm:"column:method" db:"method" json:"method" form:"method"`             //请求方法
	Result    string `gorm:"column:result" db:"result" json:"result" form:"result"`             //返回参数
}

func GetAdminLogQuery() *gorm.DB {
	return driver.Grom.Table("admin_logs")
}

func (my *AdminLog) Create() error {
	return driver.Grom.Create(my).Error
}

func (my *AdminLog) List(paginate request.Paginate, logSearch *request.LogSearch) (pagelist response.PageList, err error) {
	pagelist.CurrentPage = paginate.Page
	pagelist.PageSize = paginate.Limit
	query := GetAdminLogQuery()
	var adminlogs []AdminLog
	var total int64
	query.Count(&total)
	err = query.Order("id desc").Offset(paginate.Offset).Limit(paginate.Limit).Find(&adminlogs).Error
	pagelist.Total = total
	pagelist.Data = adminlogs
	return
}

func NewAdminLog() *AdminLog {
	return &AdminLog{}
}
